﻿Imports System.Math

<Serializable()> Public Class SvAngle

    ''' <summary>
    ''' 度.分秒 ==> 十进制
    ''' </summary>
    ''' <param name="DMS"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DmsToDeg(ByVal DMS As Double) As Double
        Dim DDMMSS As Double, D As Long, M As Long, s As Double
        Dim sn As Long
        If DMS > 0 Then
            sn = 1
        ElseIf DMS < 0 Then
            sn = -1
        Else
            sn = 0
        End If
        DDMMSS = Math.Abs(DMS)
        D = Int(DDMMSS) : DDMMSS = (DDMMSS - D) * 100
        M = Int(DDMMSS) : DDMMSS = DDMMSS - M
        s = DDMMSS * 100
        DmsToDeg = sn * (D + M / 60.0# + s / 3600.0#)
    End Function

    ''' <summary>
    ''' 十进制 ==> 度.分秒
    ''' </summary>
    ''' <param name="DEG"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DegToDms(ByVal DEG As Double) As Double
        Dim D As Long, M As Long, s As Double
        Dim sn As Long, DegAng As Double
        sn = Math.Sign(DEG)
        DegAng = Math.Abs(DEG)
        D = Int(DegAng) : DegAng = (DegAng - D) * 60
        M = Int(DegAng) : DegAng = (DegAng - M) * 60
        s = DegAng
        DegToDms = sn * (D + M / 100.0# + s / 10000.0#)
    End Function

    ''' <summary>
    ''' 十进制 ==> 弧度
    ''' </summary>
    ''' <param name="DEG"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DegToRad(ByVal DEG As Double) As Double
        DegToRad = DEG * Math.PI / 180.0#
    End Function

    ''' <summary>
    ''' 弧度 ==> 十进制
    ''' </summary>
    ''' <param name="RAD"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function RadToDeg(ByVal RAD As Double) As Double
        RadToDeg = RAD * 180.0# / Math.PI
    End Function


    ''' <summary>
    ''' 度.分秒 ==> 弧度
    ''' </summary>
    ''' <param name="DMS"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DmsToRad(ByVal DMS As Double) As Double
        DmsToRad = DegToRad(DmsToDeg(DMS))
    End Function

    ''' <summary>
    ''' 弧度  ==> 度.分秒
    ''' </summary>
    ''' <param name="RAD"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function RadToDms(ByVal RAD As Double) As Double
        RadToDms = DegToDms(RadToDeg(RAD))
    End Function

    ''' <summary>
    ''' 角度换算到 [0,2∏) 区间
    ''' </summary>
    ''' <param name="Rad"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function C0to2PI(ByVal Rad As Double) As Double
        Rad = Rad Mod PI * 2
        If Rad < 0 Then Rad = Rad + Math.PI * 2
        Return Rad
    End Function



    ''' <summary>
    ''' 角度换算到 (-∏,∏] 区间
    ''' </summary>
    ''' <param name="Rad"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function CNearZero(ByVal Rad As Double) As Double
        Rad = Rad Mod PI * 2
        If Rad <= -Math.PI Then Rad = Rad + 2 * Math.PI
        If Rad > Math.PI Then Rad = Rad - 2 * Math.PI
        Return Rad
    End Function
End Class